/*
* 受限空间作业历史
* */

import 'dart:convert';

import 'package:center_beijing/api/task_api.dart';
import 'package:center_beijing/common/styles.dart';
import 'package:center_beijing/config/global_config.dart';
import 'package:center_beijing/config/routeConfig.dart';
import 'package:center_beijing/widgets/custom_loading_widget.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:get/get.dart';
class ConfinedSpaceHistory extends StatefulWidget {
  const ConfinedSpaceHistory();

  @override
  _ConfinedSpaceHistoryState createState() => _ConfinedSpaceHistoryState();
}

class _ConfinedSpaceHistoryState extends State<ConfinedSpaceHistory> {
  EasyRefreshController _controller = EasyRefreshController();
  CustomLoading loading = CustomLoading.withMessage('请稍候...');
  List dataSource = [];
  int page = 1;
  int pageSize = 15;
  bool firstLoad = true;

  @override
  void initState() {
    super.initState();
    this.getAllReceived();
  }

  @override
  void dispose() {
    _controller.dispose();
    loading.dismiss();
    super.dispose();
  }

  /*
  * 获取受限空间作业列表
  * */
  getAllReceived() async {
    var pass = {
      'queryFields': json.encode([
        {
          "columnName": 'projectSectionId',
          "dataType": "string",
          "operator": "=",
          "queryValue": GlobalConfig.selectFactory.tag,
        }
      ]),
      'sortname':'workTime',
      'sortorder':'desc',
      'page': page,
      'limit': pageSize,
    };
    var result = await getConfinedSpaceWorkExList(pass);
    if (this.page == 1) {
      dataSource.clear();
    }
    firstLoad = false;
    if (result != null && result['hasError'] == null) {
      this.setState(() {
        if (result['results'].length == 0) {
          _controller.finishLoad(success: true, noMore: true);
        } else {
          dataSource.addAll(result['results']);
          if (dataSource.length == result['totalRecordNum']) {
            _controller.finishLoad(success: true, noMore: true);
          }
        }
      });
    } else {
      _controller.finishLoad(success: true);
    }
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text('受限空间作业'),
        ),
        body: Container(
          color: Styles.MainBackgroundColor,
          padding: EdgeInsets.fromLTRB(
              0, 0, 0, MediaQuery.of(context).padding.bottom),
          child: NotificationListener(
            onNotification: (_) {
              FocusScope.of(context).requestFocus(new FocusNode());
              return false;
            },
            child: EasyRefresh.custom(
                shrinkWrap: true,
                firstRefresh: true,
                controller: _controller,
                header: MaterialHeader(),
                footer: ClassicalFooter(
                    loadingText: '加载中',
                    loadText: '上拉加载',
                    noMoreText: '没有更多数据',
                    loadedText: '加载完成',
                    infoText: '更新于 %T',
                    textColor: Styles.NoMoreColor,
                    showInfo: false),
                emptyWidget: (dataSource.length == 0 && !firstLoad)
                    ? Center(
                  child: Image(
                    image: AssetImage('assets/images/no_data.png'),
                    width: 128,
                    height: 150,
                  ),
                )
                    : null,
                onRefresh: () async {
                  _controller.finishLoad(success: true, noMore: false);
                  if (mounted) {
                    page = 1;
                    this.getAllReceived();
                  }
                },
                onLoad: () async {
                  await Future.delayed(Duration(seconds: 2), () {
                    if (mounted) {
                      page++;
                      this.getAllReceived();
                    }
                  });
                },
                slivers: <Widget>[
                  SliverList(
                    delegate: SliverChildBuilderDelegate(
                          (context, index) {
                        return GestureDetector(
                          onTap: () {
                           Get.toNamed(RouteConfig.confinedSpaceDetail2,arguments: {
                              'businessId':this.dataSource[index]['id'],
                            });
                          },
                          child: Container(
                            color: Colors.white,
                            margin: EdgeInsets.fromLTRB(
                                0, Styles.spacing8, 0, 0),
                            padding: EdgeInsets.fromLTRB(
                                Styles.spacing12,
                                Styles.spacing8,
                                Styles.spacing12,
                                Styles.spacing8),
                            child: Column(
                              crossAxisAlignment:
                              CrossAxisAlignment.start,
                              children: [
                                Container(
                                  padding: EdgeInsets.fromLTRB(
                                      0, Styles.spacing4, 0, 0),
                                  child: Text(
                                    '${this.dataSource[index]['workName']??'--'}',
                                    style: TextStyle(
                                        color: Styles.DetailColor,
                                        fontSize:
                                        Styles.MiddleFont17),
                                  ),
                                ),
                                Container(
                                  padding: EdgeInsets.fromLTRB(
                                      0, Styles.spacing4, 0, 0),
                                  child: Text(
                                    '作业时间：${this.dataSource[index]['workTimeStr']??'--'}',
                                    style: TextStyle(
                                        color: Styles.DetailColor,
                                        fontSize:
                                        Styles.NormalFont),
                                  ),
                                ),
                                Container(
                                  padding: EdgeInsets.fromLTRB(
                                      0, Styles.spacing4, 0, 0),
                                  child: Text(
                                    '作业班组：${this.dataSource[index]['teamName']??'--'}',
                                    style: TextStyle(
                                        color: Styles.DetailColor,
                                        fontSize:
                                        Styles.NormalFont),
                                  ),
                                ),
                                Container(
                                  padding: EdgeInsets.fromLTRB(
                                      0, Styles.spacing4, 0, 0),
                                  child: Text(
                                    '所属部门：${this.dataSource[index]['deptName']??'--'}',
                                    style: TextStyle(
                                        color: Styles.DetailColor,
                                        fontSize:
                                        Styles.NormalFont),
                                  ),
                                ),
                              ],
                            ),
                          ),
                        );
                      },
                      childCount: dataSource.length,
                    ),
                  ),
                ]),
          ),
        ),);
  }
}
